/*
 * Copyright (c) 2009 Frederic Daoud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.stripesbook.rayures.view.jsp;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.servlet.jsp.tagext.BodyContent;

/**
 * An implementation of {@link javax.servlet.jsp.JspWriter} (actually, of its subclass,
 * {@link BodyContent}) that delegates most of the work to a {@link PrintWriter} that writes to a
 * {@link StringWriter}.
 *
 * <p>
 * Basically, this captures all of the output in a String, which is then returned from the
 * {@link #getString()}, {@link #toString()}, and {@link #getReader()} methods.
 * </p>
 *
 * @author Frederic Daoud
 */
public class ViewJspWriter extends BodyContent {
  private StringWriter stringWriter;
  private PrintWriter writer;

  /** Constructor with no body. */
  public ViewJspWriter() {
    super(null);
    initializeWriter();
  }

  /** Constructor with the given body. */
  public ViewJspWriter(String body) {
    this();
    writer.write(body);
  }

  /* Initializing methods */

  protected void initializeWriter() {
    stringWriter = new StringWriter();
    writer = new PrintWriter(stringWriter);
  }

  @Override
  public void clear() throws IOException {
    initializeWriter();
  }

  @Override
  public void clearBuffer() throws IOException {
    initializeWriter();
  }

  @Override
  public int getRemaining() {
    return 0;
  }

  /* Methods that return the content */

  @Override
  public String getString() {
    return toString();
  }

  @Override
  public String toString() {
    return stringWriter.toString();
  }

  @Override
  public Reader getReader() {
    return new StringReader(getString());
  }

  /* All methods below this point just delegate to the writer object. */

  @Override
  public void newLine() throws IOException {
    writer.println();
  }

  @Override
  public void print(boolean value) throws IOException {
    writer.print(value);
  }

  @Override
  public void print(char value) throws IOException {
    writer.print(value);
  }

  @Override
  public void print(int value) throws IOException {
    writer.print(value);
  }

  @Override
  public void print(long value) throws IOException {
    writer.print(value);
  }

  @Override
  public void print(float value) throws IOException {
    writer.print(value);
  }

  @Override
  public void print(double value) throws IOException {
    writer.print(value);
  }

  @Override
  public void print(char[] value) throws IOException {
    writer.print(value);
  }

  @Override
  public void print(String value) throws IOException {
    writer.print(value);
  }

  @Override
  public void print(Object value) throws IOException {
    writer.print(value);
  }

  @Override
  public void println() throws IOException {
    writer.println();
  }

  @Override
  public void println(boolean value) throws IOException {
    writer.println(value);
  }

  @Override
  public void println(char value) throws IOException {
    writer.println(value);
  }

  @Override
  public void println(int value) throws IOException {
    writer.println(value);
  }

  @Override
  public void println(long value) throws IOException {
    writer.println(value);
  }

  @Override
  public void println(float value) throws IOException {
    writer.println(value);
  }

  @Override
  public void println(double value) throws IOException {
    writer.println(value);
  }

  @Override
  public void println(char[] value) throws IOException {
    writer.println(value);
  }

  @Override
  public void println(String value) throws IOException {
    writer.println(value);
  }

  @Override
  public void println(Object value) throws IOException {
    writer.println(value);
  }

  @Override
  public void flush() throws IOException {
    writer.flush();
  }

  @Override
  public void close() throws IOException {
    writer.close();
  }

  @Override
  public void write(char[] value, int offset, int length) throws IOException {
    writer.write(value, offset, length);
  }

  @Override
  public void writeOut(Writer writer) throws IOException {
    writer.write(getString());
  }
}
